{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 设计井字棋\n",
    "\n",
    "![Asked by Google](https://img.shields.io/badge/Asked%20by-Google-important?style=social&logo=google)\n",
    "\n",
    "&emsp;&emsp;设计一种井字棋，由$2$名玩家在$n \\times n$的棋盘上对抗。**每次行棋都是有效的，有效行棋是将棋放置在网格空格上的动作**。将$n$个标记放置在水平、垂直、对角线方向上的玩家获胜，一方获胜后游戏结束、严禁继续行棋。\n",
    "\n",
    "&emsp;&emsp;以下是游戏获胜结束的示例：\n",
    "\n",
    "```text\n",
    "n = 3\n",
    "\n",
    "# 玩家1使用记号X，玩家2使用记号O\n",
    "board = TicTacToe(n)\n",
    "\n",
    "board.move(0, 0, 1) -> 0\n",
    "# X\n",
    "#         玩家1落子于(0, 0)，无人获胜\n",
    "#\n",
    "\n",
    "board.move(0, 2, 2) -> 0\n",
    "# X   O\n",
    "#         玩家2落子于(0, 2)，无人获胜\n",
    "#\n",
    "\n",
    "board.move(2, 2, 1) -> 0\n",
    "# X   O\n",
    "#         玩家1落子于(2, 2)，无人获胜\n",
    "#     X\n",
    "\n",
    "board.move(1, 1, 2) -> 0\n",
    "# X   O\n",
    "#   O      玩家2落子于(1, 1)，无人获胜\n",
    "#     X\n",
    "\n",
    "board.move(2, 0, 1) -> 0\n",
    "# X   O\n",
    "#   O      玩家1落子于(2, 0)，无人获胜\n",
    "# X   X\n",
    "\n",
    "board.move(1, 0, 2) -> 0\n",
    "# X   O\n",
    "# O O      玩家2落子于(1, 0)，无人获胜\n",
    "# X   X\n",
    "\n",
    "board.move(2, 1, 1) -> 1\n",
    "# X   O\n",
    "# O O      玩家1落子于(2, 1)，玩家1获胜\n",
    "# X X X\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TicTacToe(object):\n",
    "    def __init__(self, n):\n",
    "        self.row = [0, 0, 0]    # 行\n",
    "        self.column = [0, 0, 0] # 列\n",
    "        self.diag_1 = 0         # 对角线\n",
    "        self.diag_2 = 0         # 反对角线\n",
    "        self.n = n              # 棋盘大小\n",
    "\n",
    "    def move(self, row, column, player):\n",
    "        offset = player * 2 - 3\n",
    "        self.row[row] += offset\n",
    "        self.column[column] += offset\n",
    "        if row == column:\n",
    "            self.diag_1 += offset\n",
    "        if row + column == self.n - 1:\n",
    "            self.diag_2 += offset\n",
    "        if offset * self.n in (\n",
    "            self.row[row], self.column[column],\n",
    "            self.diag_1, self.diag_2\n",
    "        ):\n",
    "            return player\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "board = TicTacToe(3)\n",
    "board.move(0, 0, 1)\n",
    "board.move(0, 2, 2)\n",
    "board.move(2, 2, 1)\n",
    "board.move(1, 1, 2)\n",
    "board.move(2, 0, 1)\n",
    "board.move(1, 0, 2)\n",
    "print(board.move(2, 1, 1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
